package com.softWang.job.controller;

import com.softWang.common.aop.annotation.SysOperationLog;
import com.softWang.common.exception.LogicException;
import com.softWang.job.constants.SysModel;
import com.softWang.job.util.CronUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

import com.softWang.common.response.PageBean;
import com.softWang.common.request.Idparam;
import com.softWang.common.response.ResponseResult;
import com.softWang.job.po.JobManage;
import com.softWang.job.service.JobManageService;
import com.softWang.job.vo.jobManage.*;

@Api(value="定时任务管理",tags="定时任务管理")
@Validated
@RestController
@RequestMapping("/job/jobManage")
public class JobManageController {
     @Resource
     private JobManageService jobManageService;

     @PreAuthorize("@ss.hasPermi('job:manage:query')")
     @ApiOperation(value="列表(分页)")
     @RequestMapping(value = "/query", method = RequestMethod.POST)
     public ResponseResult<PageBean<JobManageQueryAllResult>> queryall(@RequestBody @Validated JobManageQueryAllParam param, HttpServletRequest request) {
         List<JobManageQueryAllResult> results=jobManageService.queryall(param);
         PageBean page=new PageBean(results);
         return ResponseResult.success(page);
     }

     @PreAuthorize("@ss.hasPermi('job:manage:add')")
     @ApiOperation(value="新增")
     @SysOperationLog(value = "新增", model = SysModel.JOB_MANAGE)
     @RequestMapping(value = "/add", method = RequestMethod.POST)
     //@Repeat(message = "重复提交")
     public ResponseResult<Long> add(@RequestBody @Validated JobManageAddParam param, HttpServletRequest request) throws Exception {
         //验证表达式是否正确
         Boolean v = CronUtils.isValid(param.getCronExpression());
         if (!v) {
             throw new LogicException("cron表达式不正确");
         }
         Long id = jobManageService.add(param);
         return ResponseResult.success(id);
     }

     @PreAuthorize("@ss.hasPermi('job:manage:edit')")
     @ApiOperation(value="修改")
     @SysOperationLog(value = "修改", model = SysModel.JOB_MANAGE)
     @RequestMapping(value = "/edit", method = RequestMethod.POST)
     public ResponseResult<Long> edit(@RequestBody @Validated JobManageEditParam param, HttpServletRequest request) throws Exception{
         //验证表达式是否正确
         Boolean v = CronUtils.isValid(param.getCronExpression());
         if (!v) {
             throw new LogicException("cron表达式不正确");
         }
         Long id = jobManageService.edit(param);
         return ResponseResult.success(id);
     }

     @PreAuthorize("@ss.hasPermi('job:manage:detail')")
     @ApiOperation(value="查看详情")
     @RequestMapping(value = "/detail", method = RequestMethod.POST)
     public ResponseResult<JobManageDetailResult> detail(@RequestBody @Validated Idparam idparam) {
         JobManageDetailResult result = new JobManageDetailResult();
         JobManage entity = jobManageService.getById(idparam.getId());
         BeanUtils.copyProperties(entity, result);
         return ResponseResult.success(result);
     }

     @PreAuthorize("@ss.hasPermi('job:manage:del')")
     @ApiOperation(value="删除")
     @SysOperationLog(value = "删除", model = SysModel.JOB_MANAGE)
     @RequestMapping(value = "/del", method = RequestMethod.POST)
     public ResponseResult del(@RequestBody @NotEmpty(message = "ids 不能为空") List<@NotNull(message = "ids 不能为空") Long> ids,
                               HttpServletRequest request) throws Exception{
         jobManageService.del(ids);
         return ResponseResult.success();
     }

    @PreAuthorize("@ss.hasPermi('job:manage:changeStatus')")
    @ApiOperation(value="修改任务状态")
    @SysOperationLog(value = "修改任务状态", model = SysModel.JOB_MANAGE)
    @PostMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestBody @Validated JobManageChangeStatusParam param) throws Exception
    {
        jobManageService.changeStatus(param);
        return ResponseResult.success();
    }

    @PreAuthorize("@ss.hasPermi('job:manage:run')")
    @ApiOperation(value="定时任务立即执行一次")
    @SysOperationLog(value = "执行一次任务", model = SysModel.JOB_MANAGE)
    @PostMapping("/run")
    public ResponseResult run(@RequestBody @Validated Idparam idparam) throws Exception
    {
        jobManageService.run(idparam.getId());
        return ResponseResult.success();
    }
}